//
//  DepartmentInfoListViewController.swift
//  PropertyCommunity
//
//  Created by benwang on 2021/7/4.
//

import UIKit
import CollectionKit

class DepartmentInfoListViewController: UIViewController {
  var collectionView = CollectionView()
  let sheetHandler = PTSheetHandle()

  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    opacityNavigationBar(color: .designKit.secondaryBackground)
  }


  override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    view.backgroundColor = .designKit.background
    view.addSubview(collectionView)

    collectionView.snp.makeConstraints { maker in
      maker.edges.equalToSuperview()
    }

    // 部门岗位
    let dataSource = ArrayDataSource<DepartmentItemModel>(
      data: DepartmentItemModel.items
    )

    let viewSource = ClosureViewSource<DepartmentItemModel, UnitImageTextView> { item, data, index in
      item.text = data.name
      item.font = .designKit.caption
      item.backgroundColor = .designKit.secondaryBackground
    }

    let sizeSource = { (index: Int, data: DepartmentItemModel, size: CGSize) in
      return CGSize(width: (size.width - 26) / 3, height: 56)
    }

    let itemProvider = BasicProvider<DepartmentItemModel, UnitImageTextView>(
      dataSource: dataSource,
      viewSource: viewSource,
      sizeSource: sizeSource,
      layout: FlowLayout(spacing: 13)
    )

    itemProvider.animator = FadeAnimator()
    itemProvider.tapHandler = { (context) in
      self.jump(to: context.data)
    }

    let positionProvider = ComposedHeaderProvider(
      layout: FlowLayout(spacing: 10).inset(by: .padding(10, 18, 15, 15)),
      headerViewSource: { (view: UILabel, data, index) in
          view.textColor = .designKit.primaryText
          view.font = .designKit.title3
          view.text = "部门岗位"
      },
      headerSizeSource: { (index, data, maxSize) -> CGSize in
        return CGSize(width: maxSize.width, height: .twoExtraLarge)
      },
      sections: [itemProvider]
    )

    /// 部门简介
    let brief = R.nib.departmentBriefContentView(owner: self)!

    let height = brief.update(text: des)

    let briefProvider = SimpleViewProvider(
        views: [brief],
        sizeStrategy: (.fill, .absolute(height))
    )

    collectionView.provider = ComposedProvider(
      layout: FlowLayout(spacing: 8),
      sections: [positionProvider, briefProvider]
    )
  }

  func jump(to item: DepartmentItemModel) {
    sheetHandler.sheet(positionBrief: des) { index in

    }
  }


  /*
   // MARK: - Navigation

   // In a storyboard-based application, you will often want to do a little preparation before navigation
   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   // Get the new view controller using segue.destination.
   // Pass the selected object to the new view controller.
   }
   */

}



struct DepartmentItemModel {
  let name: String
}

extension DepartmentItemModel {
  static let items: [DepartmentItemModel] = [
    .init(name: "主管"),
    .init(name: "部长"),
    .init(name: "职员"),
    .init(name: "主事"),
    .init(name: "会长"),
    .init(name: "前台"),
  ]
}


let des = "价管比形展议工参什众到指明发青见话活美整行步五东亲它解题此选书强然低验法用济来教两几小转片件参转非没老委那委影细团非江看些产思把就物任两真每新革。派许再火学并再参思工色家阶政热自自真样持按军现年今其增求图里及者员其或除局单山么公办拉快完矿及元主然交干南十知你内应自参完话战今响两规采日做期文段许八放。带被那群了原选线化个建队确如算格起值至京真积那山格标过机名义达片品办众基复领治作近率风民全听学义强角素提题验。行看成听民改写里太品想天任要同根门比思府量金天记之处物实红红即本切有节品节共们列外适候。温算转置手间却龙人来质完质目之马段条少几直群小任反料青证劳利相己约据对知时新查义式车安头求者被长什效例使专至水看将强交家细关劳革流六则。"
